import React, { Component } from 'react';
import { NavLink, withRouter } from 'react-router-dom'
import Header from '../compents/Header'
import Footer from '../compents/Footer'
import login from '../css/Login.module.css'
import { Input, Button } from 'antd';
import api from '../api/l_api'
class Login extends Component {
    login() {
        localStorage.getItem('token')
        let params = {
            userName: this.refs.ipt1.state.value,
            password: this.refs.ipt2.state.value
        }
        // console.log(params)
        api.login(params).then((data) => {
            console.log(data)
            if (data.code === 'success') {
                alert('登录成功')
                var userName = this.refs.ipt1.state.value
                localStorage.setItem('token', data.token)
                localStorage.setItem('userName', userName)
                console.log(userName)
                this.props.history.push('./Index')
            } else {
                alert(data.message)
            }
        })
    }
    render() {
        return (
            <div>
                <Header />
                <div className={login.div}>
                    <div className={login.cont}>
                        <div>
                            <div>
                                <h2>登录</h2>
                            </div>
                            <div className={login.mima}>
                                <div>
                                    <p>账号</p>
                                    <Input placeholder="账号" id="ipt1" ref="ipt1" />
                                </div>
                                <div>
                                    <p>密码</p>
                                    <Input placeholder="密码" id="ipt2" ref="ipt2" />
                                </div>
                                <div>
                                    <Button type="primary" onClick={this.login.bind(this)}>登录</Button>
                                </div>

                            </div>
                        </div>
                        <div className={login.register}>
                            没有账户？
                        <NavLink to="./Register">去注册</NavLink>
                        </div>
                    </div>
                </div>
                <Footer />
            </div>
        )
    }
}
export default withRouter(Login)